'''
Created on 27/03/2009

@author: eh01
'''
from im.ds import dsconstants, dsofactory
from im.ds.dso import DSO
from im.ds.dsstub import DSStub
from im.ds.dst import DST
from im.event import eventconstants
from im.event.event import Event
from im.event.eventhandler import BaseEventHandler
from im.event.eventmanager import EventManager
from xml.etree import ElementTree
from im.handler import messagetypeconstants
import httplib
import datetime
import os

class ConnectionTestHTTPResponseHandler(BaseEventHandler):
    '''
    Handles the response message for the connection test
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def do(self, data):
        dso = data
        request_id = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                                 predicate=dsconstants.PREDICATE_REQUEST_ID).get_literal()
        ds = DSStub()
        
        result = ds.get_outstanding_requests(int(request_id))
        if result:
            (http_request, lock) = result
        else:
            print 'Cannot find message entry with request_id ' + request_id
            return
        
        test_string = dso.get_dst(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                                 predicate=dsconstants.PREDICATE_ECHO_TEST_STRING).get_literal()
                                 
        if test_string is None:
            test_string = ""
                                 
        relative_path = "../../../pages/im/connectivity-test-result.html"
        
        # get the html template
        template_path = os.path.join(os.path.dirname(__file__), relative_path)
        file = open(template_path)
        html_template_string = file.read()
        html_string = html_template_string.replace('${result}', test_string)
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               'text/html', 
                                                               html_string, 
                                                               http_request, 
                                                               lock)
        event = Event(eventconstants.HTTP_RESPONSE_EVENT_TYPE, new_dso)
        EventManager().add_event(event)
        print 'End Request: ' + str(datetime.datetime.now())

class ConnectivityTestWebServiceResponseHandler(BaseEventHandler):
    
    def do(self, data):
        dso = data
                
        web_service_response_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                predicate=dsconstants.PREDICATE_WEB_SERVICE_RESPONSE)
        web_service_response = web_service_response_dst.get_literal()

        message_request_node_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_MESSAGE_REQUEST_NODE)
        
        message_request_node = message_request_node_dst.get_literal()

        echo_resp = ElementTree.XML(web_service_response)
        echo_return = echo_resp.find('.//{http://was6.webservice.spear.lx.dse.vic.gov.au}echoReturn').text

        request_id_dst = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                predicate=dsconstants.PREDICATE_REQUEST_ID)
        
        message_content_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_MESSAGE_CONTENT,
            object=dsconstants.SUBJECT_MESSAGE_CONTENT)

        echo_test_string_dst = DST(subject=dsconstants.SUBJECT_MESSAGE_CONTENT,
            predicate=dsconstants.PREDICATE_ECHO_TEST_STRING,
            literal=echo_return)
        
        message_destination_node_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_MESSAGE_DESTINATION_NODE,
            literal=message_request_node)

        message_type_dst = DST(subject=dsconstants.SUBJECT_ROOT, 
            predicate=dsconstants.PREDICATE_MESSAGE_TYPE, 
            literal=messagetypeconstants.CONNECTIVITY_TEST_RESPONSE_MESSAGE_TYPE)
                        
        new_dsts = [request_id_dst,
                message_content_dst,
                echo_test_string_dst,
                message_destination_node_dst,
                message_type_dst]
        
        new_dso = DSO(dsts=new_dsts)
        event = Event(eventconstants.SEND_TO_HUB_EVENT_TYPE, new_dso)
        EventManager().add_event(event)